package cn.favebook.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.favebook.Global;

/**
 * Servlet implementation class VCodeServlet
 */
@WebServlet("/VCode")
public class VCodeServlet extends HttpServlet {
	
	public VCodeServlet() {
		super();
	}

	public VCodeServlet(int width, int height, int codeCount, int lineCount) {
		this.width = width;
		this.height = height;
		this.codeCount = codeCount;
		this.lineCount = lineCount;
		this.createCode();
	}

	private static final long serialVersionUID = 1L;
	
	private int width = 0;
	private int height = 0;
	private int codeCount = 0;
	private int lineCount = 0;
	private String code = null;
	private BufferedImage buffImg = null;
	
	private char[] codeSeq = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 
			'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 
			'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
	
	
	
	
	public void createCode() {
		int x = 0, fontHeight = 0, codeY = 0;
		int red =0, green = 0, blue = 0;
		
		x = width / ( codeCount + 2 );
		fontHeight = height - 2;
		codeY = height - 4;
		
		buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics2d = buffImg.createGraphics();
		
		Random random = new Random();
		
		graphics2d.setColor(Color.WHITE);
		graphics2d.fillRect(0, 0, width, height);
		
		Font font = new Font("Times New Roman", Font.PLAIN, fontHeight);
		graphics2d.setFont(font);
		
		for(int i = 0; i < lineCount; i++) {
			int xs = random.nextInt(width);
			int ys = random.nextInt(height);
			int xe = xs + random.nextInt(width / 8);
			int ye = ys + random.nextInt(height / 8);
			
			red = random.nextInt(255);
			green = random.nextInt(255);
			blue = random.nextInt(255);
			graphics2d.setColor(new Color(red, green, blue));
			graphics2d.drawLine(xs, ys, xe, ye);
		}
		
		StringBuffer randomCode = new StringBuffer();
		
		for(int j = 0; j < codeCount; j++) {
			String strRandom = String.valueOf(codeSeq[random.nextInt(codeSeq.length)]);
			red = random.nextInt(255);
			green = random.nextInt(255);
			blue = random.nextInt(255);
			graphics2d.setColor(new Color(red, green, blue));
			graphics2d.drawString(strRandom, (j + 1) * x, codeY);
			randomCode.append(strRandom);
		}
		
		code = randomCode.toString();
		
	}
	
	public String getCode() {
		return code;
	}
	
	public BufferedImage getBuffImg() {  
        return buffImg;  
    }  

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		VCodeServlet vcode = new VCodeServlet(150, 35, 5, 27);
		request.getSession().setAttribute(Global.VCODE_KEY, vcode.getCode());
		response.setContentType("image/png");
		ImageIO.write(vcode.getBuffImg(), "PNG", response.getOutputStream());
	}

	


}
